package main

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"github.com/kevin186/casbin/lib"
	"github.com/kevin186/casbin/service"
	"net/http"
	"strconv"
)

func init() {
	lib.InitDb()
	//service.InitData()
	lib.InitPolicy()
}
func main() {

	//err := service.CreateRole(1, 1, "guest", "游客")
	//if err != nil {
	//	log.Fatal(err)
	//}
	//

	//service.CreateData()

	// 使用preload查询多对多关联数据
	//roles := []*models.Role{}
	//lib.DB.Preload("user").Find(&roles)
	//res, err := json.Marshal(roles)
	//if err != nil {
	//	panic(err)
	//}
	//fmt.Println(string(res))

	//users := []models.User{
	//	//UserName: "User1",
	//}
	//user := models.User{
	//	UserName: "User1",
	//}
	//err = lib.DB.Preload("Roles").Find(&users).Error
	//if err != nil {
	//	log.Fatal("Failed to query data:", err)
	//}
	//
	//res, err := json.Marshal(user)
	//if err != nil {
	//	log.Fatal("Failed to marshal data:", err)
	//}
	//fmt.Println(string(res))

	r := gin.New()
	r.Use(lib.Middlewares()...)
	r.GET("/deployment", func(ctx *gin.Context) {
		ctx.JSON(200, gin.H{"result": "部门列表"})
	})
	r.GET("/deployment/:id", func(ctx *gin.Context) {
		ctx.JSON(200, gin.H{"result": "部门详细"})
	})
	r.POST("/deployment", func(ctx *gin.Context) {
		ctx.JSON(200, gin.H{"result": "批量修改部门列表"})
	})
	r.POST("/createuser", func(ctx *gin.Context) {
		fmt.Println("创建用户")
		userName := ctx.GetHeader("user_name")
		roleName := ctx.GetHeader("role_name")

		if userName != "" {
			if roleName != "" {
				err := service.CreateUsers(userName, roleName)
				if err != nil {
					ctx.JSON(http.StatusBadRequest, gin.H{
						"msg":  "创建user失败",
						"code": 400,
					})
				}
				ctx.JSON(http.StatusOK, gin.H{
					"msg":  "创建user成功",
					"code": 200,
				})
			} else {
				err := service.CreateUsers(userName, "anonymous")
				if err != nil {
					ctx.JSON(http.StatusBadRequest, gin.H{
						"msg":  "创建用户失败",
						"code": 400,
					})
				}
				ctx.JSON(http.StatusOK, gin.H{
					"msg":  "创建默认anonymous user成功",
					"code": 200,
				})
			}
		}

	})
	r.POST("/createrouter", func(ctx *gin.Context) {
		fmt.Println("创建路由")
		routerName := ctx.GetHeader("router_name")
		routerUri := ctx.GetHeader("router_uri")
		routerMethod := ctx.GetHeader("router_method")
		if routerName != "" && routerUri != "" && routerMethod != "" {
			err := service.CreateRouters(routerName, routerUri, routerMethod)
			if err != nil {
				ctx.JSON(http.StatusBadRequest, gin.H{
					"msg":  "创建router失败",
					"code": 400,
				})
			}
			ctx.JSON(http.StatusOK, gin.H{
				"msg":  "创建router成功",
				"code": 200,
			})
		}

	})
	// 创建role时，相同角色名的codeId必须相同，例如admin Code为0, guest Code为2, 后台会递归查询相关信息
	r.POST("/createrole", func(ctx *gin.Context) {
		fmt.Println("创建角色")
		roleCode := ctx.GetHeader("role_code")
		rolePid := ctx.GetHeader("role_pid")
		roleName := ctx.GetHeader("role_name")
		roleComment := ctx.GetHeader("role_comment")
		routerName := ctx.GetHeader("router_name")
		routerMethod := ctx.GetHeader("router_method")
		roleCodeInt, err := strconv.Atoi(roleCode)
		if err != nil {
			fmt.Println("roleCode转换数字失败")
			return
		}
		var rolePidInt int
		if rolePid != "" {
			rolePidInt, err = strconv.Atoi(rolePid)
			if err != nil {
				fmt.Println("rolePidInt转换数字失败")
				return
			}
		}

		if roleCode != "" && roleName != "" && roleComment != "" && routerName != "" {
			err := service.CreateRole(roleCodeInt, rolePidInt, roleName, roleComment, routerName, routerMethod)
			if err != nil {
				ctx.JSON(http.StatusBadRequest, gin.H{
					"msg":  "创建role失败",
					"code": 400,
				})
			}
			ctx.JSON(http.StatusOK, gin.H{
				"msg":  "创建role成功",
				"code": 200,
			})
		}

	})

	// 删除角色
	r.POST("/deleterolerouter", func(ctx *gin.Context) {
		roleName := ctx.GetHeader("role_name")
		routerName := ctx.GetHeader("router_name")
		routerMethod := ctx.GetHeader("router_method")
		//routerId := ctx.GetHeader("router_id")
		if roleName != "" && routerName != "" {
			role, err := service.DeleteRoleRouter(roleName, routerName, routerMethod, 1)
			if err != nil {
				ctx.JSON(http.StatusBadRequest, gin.H{
					"msg":  "创建role失败",
					"code": 400,
				})
			} else {
				ctx.JSON(http.StatusOK, gin.H{
					"msg":  fmt.Sprintf("删除role: %s 成功", role),
					"code": 200,
				})
			}

		}
	})

	// 删除路由
	r.POST("/deleterouter", func(ctx *gin.Context) {
		routerName := ctx.GetHeader("router_name")
		routerMethod := ctx.GetHeader("router_method")
		//routerId := ctx.GetHeader("router_id")
		if routerName != "" && routerMethod != "" {
			router, err := service.DeleteRouter(routerName, routerMethod)
			if err != nil {
				fmt.Println(err.Error())
				ctx.JSON(http.StatusBadRequest, gin.H{
					"msg":  "创建router失败",
					"code": 400,
				})
			}
			ctx.JSON(http.StatusOK, gin.H{
				"msg":  fmt.Sprintf("删除router: %s 成功", router.RouterName),
				"code": 200,
			})
		} else {
			ctx.JSON(http.StatusBadRequest, gin.H{
				"msg":  "创建router失败",
				"code": 400,
			})
		}
	})

	// 为角色添加访问某个路由的权限
	r.POST("/createrolerouter", func(ctx *gin.Context) {
		roleName := ctx.GetHeader("role_name")
		routerName := ctx.GetHeader("router_name")
		routerMethod := ctx.GetHeader("router_method")
		if err := service.CreateRoleRouter(roleName, routerName, routerMethod); err != nil {
			fmt.Println(err.Error())
			ctx.JSON(http.StatusBadRequest, gin.H{
				"msg":  "创建 createrolerouter 失败",
				"code": 400,
			})
		}
		ctx.JSON(http.StatusBadRequest, gin.H{
			"msg":  "创建 createrolerouter 成功",
			"code": 200,
		})

	},
	)
	r.Run(":8080")

}
